package com.ruoyi.system.mongo.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mongodb.WriteResult;
import com.mongodb.client.result.UpdateResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.mongodb.AllSchool;
import com.ruoyi.system.domain.mongodb.Klasses;
import com.ruoyi.system.domain.mongodb.Schools;
import com.ruoyi.system.mongo.AllSchoolDao;
import javafx.util.Pair;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author yangtao
 * @since 2021-02-24 11:22
 */
@Component
public class AllSchoolDaoImpl implements AllSchoolDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MongoOperations mongoOperations;

    @Override
    public List<AllSchool> selectSchoolByCode(String eareCode) {
        Query query = new Query(Criteria.where("eareCode").is(eareCode));
        List<AllSchool> allSchools = mongoTemplate.find(query, AllSchool.class,"schools");
        return allSchools;
    }

    @Override
    public List<Klasses> selectClassList(String schoolId) {
        Query query = new Query(Criteria.where("school").is(new ObjectId(schoolId)));
        List<Klasses> classList = mongoTemplate.find(query, Klasses.class,"klasses");
        return classList;
    }

    @Override
    public AllSchool queryByNames(String names) {
        Query query = new Query(Criteria.where("names").is(names));
        AllSchool schools = mongoTemplate.findOne(query, AllSchool.class,"schools");
        return schools;
    }

    @Override
    public AllSchool queryByNamesAndCode(String names, String eareCode) {
        Query query = new Query(Criteria.where("names").is(names).and("eareCode").is(eareCode));
        AllSchool schools = mongoTemplate.findOne(query, AllSchool.class,"schools");
        return schools;
    }

    @Override
    public AllSchool queryById(String schoolId) {
        Query query = new Query(Criteria.where("_id").is(schoolId));
        AllSchool schools = mongoTemplate.findOne(query, AllSchool.class,"schools");
        return schools;
    }

    @Override
    public Schools save(Schools schools) {
         return mongoTemplate.insert(schools);
    }

    @Override
    public PageInfo<Schools> selectSchoolAll(String schoolName,String eareCode,Integer pageNum,Integer pageSize) {
        Sort sort = Sort.by(Sort.Direction.ASC, "_id");
        if(pageNum != 0){
            pageNum = pageNum/pageSize;
        }
        Pageable pageable = PageRequest.of(pageNum, pageSize, sort);
        Query query = new Query();
        //计算总数
        //动态拼接查询条件
        if (StringUtils.isNotEmpty(schoolName)){
            Pattern pattern = Pattern.compile("^.*" + schoolName + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("names").regex(pattern));
        }
        if (StringUtils.isNotEmpty(eareCode)){
            Pattern pattern = Pattern.compile("^.*" + eareCode + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("eareCode").regex(pattern));
        }
        long total = mongoTemplate.count(query, Schools.class,"schools");
        List<Schools> schoolsList = mongoTemplate.find(query.with(pageable), Schools.class,"schools");
        PageInfo<Schools> pageInfo = new PageInfo<>();
        pageInfo.setList(schoolsList); //结果集
        pageInfo.setTotal(total);  //总条数
        pageInfo.setPageNum(pageNum);//页码
        pageInfo.setPageSize(pageSize);//条数
        return pageInfo;
    }

    @Override
    public void deleteSchoolById(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,Schools.class,"schools");
    }

    @Override
    public Integer updateSchool(String schoolId, String schoolName, String eareCode) {
    /*
        Query query1 = new Query();
        Pattern pattern = Pattern.compile("^.*襄樊.*$", Pattern.CASE_INSENSITIVE);
        query1.addCriteria(Criteria.where("names").regex(pattern));
        List<Schools> schoolsList = mongoTemplate.find(query1, Schools.class,"schools");
        System.out.println("schoolsList  大小：" + schoolsList.size());
        if (schoolsList != null) {
            for (Schools schools : schoolsList) {
                Query query = new Query();
                query.addCriteria(Criteria.where("_id").is(new ObjectId(schools.getId())));  //_id区分引号 "1"和1
                String newName =  schools.getNames().replaceAll("襄樊","襄阳");
                Update update = Update.update("names", newName);
                UpdateResult upsert = mongoTemplate.upsert(query, update, "schools");
                System.out.println("修改：" + upsert.getModifiedCount());
            }
        }*/
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(new ObjectId(schoolId)));  //_id区分引号 "1"和1
        Update update = new Update();
        if (!StringUtils.isEmpty(schoolName))
            update.set("names", schoolName); //更新学校名称
        if (!StringUtils.isEmpty(eareCode))
            update.set("eareCode", eareCode); //更新邮政编码
        UpdateResult upsert = mongoTemplate.upsert(query, update, "schools");
        Long modifiedCount = upsert.getModifiedCount();
        return modifiedCount.intValue();
    }
}